import { Component, OnInit } from '@angular/core';
import { Record} from '../record';
import { RecordsService} from '../service/records.service';
import {BooksService} from '../service/books.service';
import {PeopleService} from '../service/people.service';
import {Book} from '../book';
import {People} from '../people';

@Component({
  selector: 'app-second-lend',
  templateUrl: './second-lend.component.html',
  styleUrls: ['./second-lend.component.css']
})
export class SecondLendComponent implements OnInit {

  book: Book[];
  people: People[];
  temp: People;
  tip: string;
  bid: string;
  date: Date;
  tempYear: number;
  tempMonth: number;
  tempDay: number;
  realMonth: string;

  onSubmit() {
    this.returnWithMessage(this.bid);
  }

  returnWithMessage(book_id: string): Promise<Book> {
    return this.booksService.getBookBybid(book_id)
      .then(book => {
        const that = this;
        const auth = new Book();
        if (book_id === undefined) {
          this.tip = '图书编号不能为空';
          setTimeout(function () {
            that.tip = null;
          }, 1000);
        } else if (book_id !== undefined && book.valueOf().toString() === '') {
          this.tip = '编号错误';
          setTimeout(function () {
            that.tip = null;
          }, 1000);
        } else if (book_id === book[0].bid) {
          this.temp = JSON.parse(sessionStorage.getItem('usr'));
          this.recordsService.getRecordsOfNotReturn(book_id, this.temp.pid).then(records => {
            if (records.valueOf().toString() === '') {
              that.tip = '你没有借这本书';
              setTimeout(function () {
                that.tip = null;
              }, 1000);
            } else {
              that.tip = '续借成功';
              setTimeout(function () {
                that.tip = null;
              }, 1000);
              const formatDate = ( time: any ) => {
                // 格式化日期，获取今天的日期
                const Dates = new Date( time );
                const year: number = Dates.getFullYear();
                const month: any = ( Dates.getMonth() + 1 ) < 10 ? '0' + ( Dates.getMonth() + 1 ) : ( Dates.getMonth() + 1 );
                const day: any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
                return year + '-' + month + '-' + day;
              };
              const afterSixty = ( time: any ) => {
                // 格式化日期，获取30天后日期
                const Dates = new Date( time );
                const year: number = Dates.getFullYear();
                const month: any = ( Dates.getMonth() + 1 ) < 10 ? '0' + ( Dates.getMonth() + 1 ) : ( Dates.getMonth() + 1 );
                const day: any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
                this.tempYear = year;
                this.tempMonth = month;
                this.tempDay = day;
                if(this.tempMonth === 11){
                  this.tempMonth = 1;
                  this.tempYear += 1;
                }else if(this.tempMonth === 12){
                  this.tempYear += 1;
                  this.tempMonth = 2;
                }else{
                  this.tempMonth += 2;
                }

                if(this.tempMonth < 10){
                  this.realMonth = '0' + this.tempMonth;
                }
                return this.tempYear + '-' + this.realMonth + '-' + this.tempDay;
              };

              records[0].rdate = afterSixty(this.date);
              this.recordsService.updateRDate(records[0]);
              console.log("距离借书日期60天后："+records[0].rdate);
              return records;
            }
          });
        }
        return auth;
      })
      .catch(this.handleError);
  }


  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error);
    return Promise.reject(error.message || error);
  }

  constructor(private booksService: BooksService, private peopleService: PeopleService
              , private recordsService: RecordsService) { }

  ngOnInit() {
    this.date = new Date();
  }

}
